<template>
  <div>
    <q-form class="sc-edit">
      <yq-table
        v-model:search="searchForm"
        v-model:table="tableOptions"
        v-model:initial="initialPagination"
        @onRequest="onRequest"
        @add="add"
        @search="getList"
      >
        <template v-slot:yq_table_body="props">
          <q-tr :props="props.props">
            <q-td key="member_id" :props="props.props">
              <span>{{ props.props.row.member_id }}</span>
            </q-td>

            <q-td key="username" :props="props.props">
              <span>{{ props.props.row.username }}</span>
            </q-td>

            <q-td key="password" :props="props.props">
              <span>{{ props.props.row.password }}</span>
            </q-td>

            <q-td key="nickname" :props="props.props">
              <span>{{ props.props.row.nickname }}</span>
            </q-td>

            <q-td key="mobile" :props="props.props">
              <span>{{ props.props.row.mobile }}</span>
            </q-td>

            <q-td key="avatar" :props="props.props">
              <q-avatar v-if="props.props.row.avatar">
                <el-image
                  style="width: 50px; height: 50px"
                  :src="props.props.row.avatar"
                  :preview-src-list="[props.props.row.avatar]"
                  hide-on-click-modal
                  preview-teleported
                  :initial-index="1"
                  fit="cover"
                />
              </q-avatar>
            </q-td>

            <q-td key="status" :props="props.props">
              <span>{{ props.props.row.status == 0 ? '关闭' : '启动' }}</span>
            </q-td>

            <q-td key="operation" :props="props.props">
              <q-btn
                flat
                round
                dense
                color="primary"
                icon="edit"
                @click="edit(props.props.row.id, props.props.row)"
                v-permission="'/member/List:detail'"
              >
                <q-tooltip>编辑</q-tooltip>
              </q-btn>
              <q-btn
                flat
                round
                dense
                class="q-mx-xs"
                color="negative"
                icon="delete"
                v-permission="'/member/List:delete'"
              >
                <q-menu
                  transition-show="jump-up"
                  transition-hide="jump-down"
                  content-class="text-center"
                  self="bottom left"
                  anchor="top left"
                  :offset="[60, 5]"
                >
                  <div class="q-px-sm q-pt-md">
                    <q-icon name="info" color="warning" class="q-mr-sm" />
                    是否要删除此行?
                  </div>
                  <div class="q-gutter-x-sm q-pt-sm q-pb-md">
                    <q-btn
                      color="grey-6"
                      unelevated
                      class="no-border-radius text-body2"
                      padding="2px sm"
                      dense
                      label="取消"
                      v-close-popup
                    ></q-btn>
                    <q-btn
                      color="primary"
                      unelevated
                      class="no-border-radius text-body2"
                      padding="2px sm"
                      dense
                      label="确认"
                      @click="deleteData(props.props.row.id, props.props.row)"
                      v-close-popup
                    ></q-btn>
                  </div>
                </q-menu>
              </q-btn>
            </q-td>
          </q-tr>
        </template>
      </yq-table>
    </q-form>
    <yq-dialog v-model:lockDialog="lockDialog" :title="title">
      <template v-slot:yq_dialog_body
        ><list-dialog :form="formData" @backList="getList"
      /></template>
    </yq-dialog>
  </div>
</template>

<script setup lang="ts">
import { memberData, pageInit } from '../mocklist';
import { memberList, memberDelete, memberDetail } from '@/api/member';
import { Ref } from 'vue';
import { useQuasar } from 'quasar';
import ListDialog from './ListDialog.vue';
import { cloneDeep } from 'lodash-es';

interface formProps {
  member_id?: number;
  username?: string; //用户账号
  password?: string; //用户密码
  nickname?: string; //用户昵称
  mobile?: string; //电话
  avatar?: string; //头像
  status?: number; //状态
}

const tableOptions = reactive({
  tableLoading: false,
  ...memberData,
});

const initialPagination = ref(cloneDeep(pageInit));
const lockDialog = ref(false);
const treeDialog = ref(false);
const title = ref('');
const formData: Ref<formProps> = ref({});
const $q = useQuasar();
const searchForm = reactive({
  keywords: '',
  search: {
    status: '',
  },
  dateMin: {
    created_at: '',
  },
  dateMax: {
    created_at: '',
  },
});
//添加
const add = () => {
  title.value = '新建会员';
  formData.value = { status: 1 };
  lockDialog.value = true;
};

const query = () => {
  getList();
};

const onRequest = (props: any) => {
  const { page, rowsPerPage, rowsNumber } = props.pagination;
  initialPagination.value.page = page;
  initialPagination.value.rowsPerPage = rowsPerPage;
  initialPagination.value.per_page = rowsPerPage;
  initialPagination.value.rowsNumber = rowsNumber;
  getList();
};

//编辑
const edit = async (index: number, item: any) => {
  console.log('edit', item);
  lockDialog.value = true;
  title.value = '编辑会员';
  const detail: any = await memberDetail({ member_id: item.member_id });
  let data = cloneDeep(detail.data);
  formData.value = data;
};

//删除
const deleteData = async (index: number, item: any) => {
  const res: any = await memberDelete({ member_id: item.member_id });
  if (res.code == 200) {
    $q.notify({
      color: 'white',
      textColor: 'positive',
      icon: 'check_circle',
      position: 'top',
      message: res.msg,
    });
    getList();
  }
};

//获取列表
const getList = async () => {
  lockDialog.value = false;
  treeDialog.value = false;
  tableOptions.tableLoading = true;
  const body = cloneDeep(Object.assign(initialPagination.value, searchForm));
  const res: any = await memberList(body);
  tableOptions.tableLoading = false;
  tableOptions.columnDatas = res.data.data;
  initialPagination.value.rowsNumber = res.data.total;
};
onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.sc-design .sc-edit .q-item__section--side {
  padding-right: 0;
}

.q-field--dense .q-field__control {
  padding: 0 5px;
  height: 28px;
  .q-field__marginal {
    height: 28px;
  }
}
.q-field--auto-height .q-field--dense .q-field__control,
.q-field__native {
  min-height: 28px;
}
.q-gutter-x-sm,
.q-gutter-sm {
  margin-left: 15px;
}
::v-deep(.my-special-class) {
  height: 48px;
  background: #fafafa;
}
::v-deep(.my-special-class th) {
  font-size: 16px;
}
</style>
